package cn.king.org.service.impl;

import cn.king.basic.exception.BusinessException;
import cn.king.basic.util.BaiduAiUtils;
import cn.king.org.domain.Employee;
import cn.king.org.domain.Shop;
import cn.king.basic.service.impl.BaseServiceImpl;
import cn.king.org.domain.ShopAuditLog;
import cn.king.org.dto.ShopDto;
import cn.king.org.mapper.EmployeeMapper;
import cn.king.org.mapper.ShopAuditLogMapper;
import cn.king.org.mapper.ShopMapper;
import cn.king.org.service.ShopServiceI;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopServiceI {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 店铺入驻校验
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        //1.校验
        //空值校验
        if(StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getTel())){
            throw new BusinessException("信息不完整！");
        }
        //两次密码不一致校验
        if(!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())){
            throw new BusinessException("两次密码不一致！");
        }
        //当前店铺是否入驻过
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if(dbShop != null){
            throw new BusinessException("该店铺已入驻！请重新确认入驻店铺信息！");
        }

        //2.百度AI只能审核【店铺名，logo是否合法】
        //审核店铺名
        if(!BaiduAiUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名称违规！请修改后重新入驻！");
        }
        //审核logo
        if(!BaiduAiUtils.ImgCensor("http://123.207.27.208" + shop.getLogo())){
            throw new BusinessException("店铺Logo违规！请修改后重新入驻！");
        }

        //3.添加到数据库
        //先添加员工信息
        Employee admin = shop.getAdmin();
        employeeMapper.add(admin); //这个时候的shop_id为null
        //再添加店铺信息
        shop.setAdminId(admin.getId());
        shopMapper.add(shop);
        //将店铺id更新到员工表中
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
        //4.人工审核之后发送激活邮件
    }

    /**
     * 审核通过
     * @param log
     */
    @Override
    public void auditPass(ShopAuditLog log) throws MessagingException {
        //1.改状态：1待审核 2审核通过待激活
        shopMapper.updateState(2,log.getShopId());
        //2.添加审核日志
        log.setState(1); //审核通过
        log.setShopId(log.getShopId());
        shopAuditLogMapper.add(log);
        //3.发邮件给店长通知激活
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2097862548@qq.com"); //平台的邮箱
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.getOne(log.getShopId());
        System.out.println(shop);
        helper.setText("<h1>你的店铺审核已通过!!!</h1><img src='http://123.207.27.208"+
                shop.getLogo() +"' ><a href='http://172.16.6.251:8080/shop/active/"+
                shop.getId() +"'>点击该链接激活</a>",true);
        //收件人
        Employee employee = employeeMapper.getOne(shop.getAdminId());
        System.out.println(employee.getEmail());
        helper.setTo(employee.getEmail()); //客户的邮箱
        javaMailSender.send(mimeMessage);
    }

    /**
     * 审核失败
     * @param log
     */
    @Override
    public void auditReject(ShopAuditLog log) {
        //1.改状态：1待审核 4审核失败-驳回
        shopMapper.updateState(4,log.getShopId());
        //2.添加审核日志
        log.setState(0); //审核失败
        log.setShopId(log.getShopId());
        shopAuditLogMapper.add(log);
        //3.发邮件给店长通知修改信息重新申请入驻
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("2097862548@qq.com");
        //邮件主题
        mailMessage.setSubject("店铺审核未通过通知");
        //邮件内容：普通文件无法解析html标签
        Shop shop = shopMapper.getOne(log.getShopId());
        mailMessage.setText("你的店铺未通过，请核对信息后重新入驻！");
        //收件人
        Employee employee = employeeMapper.getOne(shop.getAdminId());
        mailMessage.setTo(employee.getEmail()); //客户的邮箱
        //发送邮件
        javaMailSender.send(mailMessage);
    }

    /**
     * 激活店铺
     * @param id
     */
    @Override
    public void active(Long id) {
        shopMapper.updateState(3,id);
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }
}
